package com.example.yuejian23.yuejian_23.Fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.yuejian23.yuejian_23.Adapter.PeixunAdapter;
import com.example.yuejian23.yuejian_23.R;
import com.example.yuejian23.yuejian_23.javaBean.PeixunBean;
import com.example.yuejian23.yuejian_23.service.PeixunService;


import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by dong on 2017/10/10.
 */

public class PeixunFragment extends Fragment
{

    RecyclerView recyclerView;
    View view = null;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view=inflater.inflate(R.layout.tab01,container,false);

        showData();
        initView();
        return view;
    }

    private void initView() {
        recyclerView =(RecyclerView) view.findViewById(R.id.peixun);
        recyclerView.setLayoutManager(new LinearLayoutManager(PeixunFragment.this.getContext()));
    }

    private void showData()
    {

        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://115.159.93.244/new18yuejian/api.php/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        PeixunService peixunService=retrofit.create(PeixunService.class);

        Call<List<PeixunBean>> call=peixunService.getList();

        call.enqueue(new Callback<List<PeixunBean>>() {
            @Override
            public void onResponse(Call<List<PeixunBean>> call, Response<List<PeixunBean>> response) {
                List<PeixunBean> peixun=response.body();

                recyclerView.setAdapter(new PeixunAdapter(getActivity(),peixun));

            }

            @Override
            public void onFailure(Call<List<PeixunBean>> call, Throwable t) {

            }
        });

    }
}